#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# remember the start time
echo $(date +%s) > gsas_start.txt

# test the GSAS installation
if [ "$gsas" = "" ]; then
	if [ "$GSAS" = "" ]; then
		echo "Your environment variable <gsas> is not set! (gsas=<$gsas>, GSAS=<$GSAS>)"
		echo "GSAS will not be able to find atom parameters etc."
		echo "You need to set it to point to the path with your GSAS installation, e.g. GSAS=C:\GSAS or so."
		echo "See the GSAS readme.txt file for more details."
		read -p "Hit Ctrl-c to abort."
		exit 0
	else
		# we found the capital version, warn about Unix GSAS wanting the small letter version
		if [ "$OS" = "Windows_NT" ]; then
			echo "I think I run on a Windows box... "
			echo "No worries about capital or small letters for GSAS environment variable..."
		else
			echo "Your environment variable GSAS should be in small letters"
			echo "on a UNIX system for GSAS to recognize it! Ignore this "
			echo "message on a Windows system (I think this is NOT a Windows."
			echo "system...)"
			read -p "Hit Ctrl-c to abort."
		fi
	fi
else
	echo "Environment variable for GSAS found. "
fi

if [ "$PGPLOT_FONT" = "" ]; then
	if [ "$pgplot_font" = "" ]; then
		echo "Your environment variable <PGPLOT_FONT> is not set!"
		echo "GSAS will not be able to find fonts for the plotting routines."
		echo "You need to set it to point to the path with the grfont.dat file in your GSAS installation, e.g. PGPLOT_FONT=C:\GSAS\PGL\GRFONT.DAT or so."
		echo "See the GSAS readme.txt file for more details."
		read -p "Hit Ctrl-c to abort."
		exit 0
	else
		echo "Environment variable PGPLOT found. "
	fi
else
	echo "Environment variable for PGPLOT found. "
fi

gsas_path=`which expedt`
if [ "$gsas_path" = "" ]; then
	echo "Your environment variable <PATH> is not set to include the GSAS programs!"
	echo "GSAS will not be able to work at all!"
	echo "You need to set it to point to the path with the executable files in your GSAS installation, e.g. PATH=...;C:\GSAS\EXE or so."
	echo "See the GSAS readme.txt file for more details."
	read -p "Hit Ctrl-c to abort."
	exit 0
else
	echo "Path includes GSAS executables. "
fi

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without name for EXP file!"
	read -p "Hit Ctrl-c to abort."
	exit 0
fi

if [ "$2" = "" ]; then
	echo "$0 called without title for EXP file!"
	read -p "Hit Ctrl-c to abort."
	exit 0
fi


# Define the experiment file in this shell
echo `echo $1 | tr "[:lower:]" "[:upper:]"`>GSAS_EXP

# Create the LaTeX document
echo "\documentclass{report}" > `cat GSAS_EXP`.tex
echo "\usepackage{fullpage}" >> `cat GSAS_EXP`.tex
echo "\usepackage{graphicx}" >> `cat GSAS_EXP`.tex
echo "\begin{document}" >> `cat GSAS_EXP`.tex
echo "\begin{center}" >> `cat GSAS_EXP`.tex
echo "\verb=$2=" >> `cat GSAS_EXP`.tex
echo "\end{center}" >> `cat GSAS_EXP`.tex
echo " " >> `cat GSAS_EXP`.tex
echo "\section{Initialization}" >> `cat GSAS_EXP`.tex

echo "GSAS EXP file is \verb=`cat GSAS_EXP`.EXP=.\newline" >> `cat GSAS_EXP`.tex

# Delete old stuff
rm `cat GSAS_EXP`.EXP 2>/dev/null
rm `cat GSAS_EXP`.O*  2>/dev/null
rm `cat GSAS_EXP`.R*  2>/dev/null
rm `cat GSAS_EXP`.P*  2>/dev/null
rm `cat GSAS_EXP`.LST 2>/dev/null
rm `cat GSAS_EXP`.CMT 2>/dev/null
rm `cat GSAS_EXP`.pdf 2>/dev/null
rm `cat GSAS_EXP`.ps  2>/dev/null
rm `cat GSAS_EXP`.log 2>/dev/null
rm `cat GSAS_EXP`.dvi 2>/dev/null
rm `cat GSAS_EXP`.aux 2>/dev/null
rm gsas_step.txt      2>/dev/null
rm temp.tex           2>/dev/null
rm gsas_print.txt     2>/dev/null

echo "Deleted older temporary files.\newline" >> `cat GSAS_EXP`.tex

# test if the EXP file was deleted
if [ -e `cat GSAS_EXP`.EXP ]; then
	echo "Could not delete `cat GSAS_EXP`.EXP - this will cause trouble!"
	echo "Please check if the file is write-protected or opened by another application."
	echo "Restart the script after fixing this problem."
	read -p "Hit Ctrl-c to abort."
	exit 0
else
	echo "File `cat GSAS_EXP`.EXP does not exist, creating a new one."
fi

# Create the new file
echo "y" > temp.txt
echo $2 >> temp.txt
echo "x" >> temp.txt
expedt `cat GSAS_EXP` < temp.txt > out.txt
echo "Your new experiment file is \verb=`cat GSAS_EXP`.EXP=.\newline" >> `cat GSAS_EXP`.tex
echo "Experiment title set to \verb=$2=.\newline" >> `cat GSAS_EXP`.tex
